﻿using FE.Shared;
using Renci.SshNet;
using System;
using System.IO;
using System.Linq;

namespace FE.Common
{
    public static class SftpHelper
    {
        public static void ConnTest(SftpConfig config)
        {
            using (var sftp = new SftpClient(config.Host, config.UserName, config.Password))
            {
                sftp.Connect();
                var lst = sftp.ListDirectory(config.Path);
                var list = lst.ToList();
                sftp.Disconnect();
            }
        }

        private static void Download(string localFilePath, string sftpFilePath)
        {
            BuildOperation(localFilePath, sftpFilePath, Down);
        }

        private static void Upload(string localFilePath, string sftpFilePath)
        {
            BuildOperation(localFilePath, sftpFilePath, Upload);
        }

        public static void Upload(SftpConfig config)
        {
            BuildOperation(config, UploadAction);
        }

        public static void Download(SftpConfig config)
        {
            BuildOperation(config, DownloadAction);
        }

        private static void UploadAction(string path, SftpClient sftp)
        {
            using (var file = new FileStream(path, FileMode.OpenOrCreate))
            {
                sftp.UploadFile(file, path);
            }
        }

        private static void DownloadAction(string path, SftpClient sftp)
        {
            using (var file = new FileStream(path, FileMode.OpenOrCreate))
            {
                sftp.DownloadFile(path, file);
            }
        }

        private static void BuildOperation(SftpConfig config, Action<string, SftpClient> callbackAction)
        {
            using (var sftp = new SftpClient(config.Host, config.UserName, config.Password))
            {
                sftp.Connect();
                callbackAction(config.Path, sftp);
                sftp.Disconnect();
            }
        }

        private static void BuildOperation(string localFilePath, string sftpFilePath,
            Action<string, string, SftpClient> callbackAction)
        {
            using (var sftp = new SftpClient("192.168.137.132", "mysftp", "abcd1234"))
            {
                try
                {
                    sftp.Connect();
                    callbackAction(localFilePath, sftpFilePath, sftp);
                    sftp.Disconnect();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                finally
                {
                    sftp.Disconnect();
                }
            }
        }

        private static void Down(string path, string output, SftpClient sftp)
        {
            using (var file = new FileStream(path, FileMode.Create))
            {
                sftp.DownloadFile(output, file);
            }
        }

        private static void Upload(string path, string output, SftpClient sftp)
        {
            using (var file = new FileStream(path, FileMode.Open))
            {
                sftp.UploadFile(file, output);
            }
        }
    }
}